package com.etc.controller;

import com.etc.pojo.Car;
import com.etc.pojo.User;
import com.etc.service.UserServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    UserServiceImpl userService;

    @Autowired
    RabbitTemplate rabbitTemplate;


    @RequestMapping("/")
    public String showRegister(){
        return "add";
    }

    @RequestMapping("add")
    public String add(User user){
        user.setTime(new Date());
        userService.add(user);
        return "redirect:/user/lo";
    }

    @RequestMapping("lo")
    public String lo(){
        return "login";
    }


    @RequestMapping("login")
    public String login(Model model, User user, HttpSession session){
        User user1=userService.login(user);

        if (user1!=null){
            if (user1.getRole().equals(1)){
                session.setAttribute("user1",user1);
                return "redirect:/user/listUser1";
            }
            if (user1.getRole().equals(2)){
                session.setAttribute("user2",user1);
                return "redirect:/user/listUser2";
            }
        }
        return "add";
    }

    @RequestMapping("listUser1")
    public String listUser1(Integer id,Model model){
        List<Car> list=userService.listUser1();
        model.addAttribute("list",list);
        return "list1";
    }

    @RequestMapping("listUser2")
    public String listUser2(Model model){
        List<Car> list=userService.listUser1();
        model.addAttribute("list",list);
        return "list2";
    }

    @RequestMapping("del")
    public String del(Integer id){
        userService.del(id);
        return "redirect:/user/listUser1";

    }

    @RequestMapping("caradd")
    public String caradd(Car car,Model model){
        userService.caradd(car);
        List<Car> list=userService.listUser1();
        model.addAttribute("list",list);
        return "list1";
    }

    @RequestMapping("showId")
    public String showId(Integer id,Model model){
        Car car=userService.showId(id);
        model.addAttribute("list",car);
        return "update";
    }

    @RequestMapping("update")
    public String update(Car car,Model model){
        userService.update(car);
        List<Car> list=userService.listUser1();
        model.addAttribute("list",list);
        return "list1";
    }

    @RequestMapping("car")
    public String caradd(){
        return "caradd";
    }


    @RequestMapping("ti")
    public String ti(Integer id){
        Car car=userService.ti(id);
        rabbitTemplate.convertAndSend("news-exchange","province.city.street.shop",car.getName()+car.getPai()+car.getPrice());
        return "redirect:/user/listUser2";
    }

    @RequestMapping("mohu")
    public String mohu(Car car,Model model){
        List<Car>list=userService.mohu(car);
        model.addAttribute("list",list);
        return "list1";
    }

    @RequestMapping("mohu2")
    public String mohu2(Car car,Model model){
        List<Car>list=userService.mohu(car);
        model.addAttribute("list",list);
        return "list2";
    }


















}
